<template>
  <!-- <el-container>：外层容器。当子元素中包含 <el-header> 等组件时，全部子元素会垂直上下排列，否则会水平左右排列。 -->
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu class="el-menu-top" mode="horizontal" background-color="#23262E" text-color="#fff" active-text-color="#409EFF">
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img :src="userInfo && userInfo.user_pic?userInfo.user_pic:defaultAvator" alt="" class="avatar" />

            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="logout"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <img :src="userInfo && userInfo.user_pic?userInfo.user_pic:defaultAvator" alt="" />
          <span>欢迎{{userInfo && userInfo.nickname?userInfo.nickname:userInfo.username}}</span>

        </div>
        <!-- 左侧菜单 -->
        <!-- 动态绑定目前的路由 ，刷新页面以后高亮保存在这个路径，不会到默认的首页,例如(/userpwd)-->
        <el-menu :default-active="$route.path" class="el-menu-vertical-demo" background-color="#23262E" text-color="#fff" active-text-color="#409EFF" unique-opened router>
          <!-- template标签上面不需要添加key,因为它不会被渲染 -->
          <template v-for="(item,i) in menus">
            <!-- 菜单选项的index是菜单的唯一身份，并且可以作为路由的链接 -->
            <!-- 不包含子菜单的“一级菜单” -->
            <el-menu-item :key="i" v-if="!item.children" :index="item.indexPath"><i class="el-icon-s-tools"></i>{{item.title}}</el-menu-item>
            <!-- 包含子菜单的“一级菜单” -->
            <el-submenu :key="i" v-else :index="item.indexPath">
              <template slot="title">
                <i class="el-icon-s-tools"></i>
                <span>{{item.title}}</span>
              </template>
              <!-- 二级菜单渲染 -->
              <el-menu-item v-for="(menu,n) in item.children" :index="menu.indexPath"><i :class="menu.icon"></i>{{menu.title}}</el-menu-item>

            </el-submenu>

          </template>

        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <router-view />
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState, } from 'vuex'
//用webpack来操作路径
import defaultAvator from '@/assets/logo.png'
export default {
  name: 'Home',
  data () {
    return {
      defaultAvator,
      menus: []
    }
  },
  computed: {
    ...mapState(['userInfo', 'token'])
  },
  methods: {
    logout () {
      this.$confirm('此操作将退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //清空token
        this.$store.commit('updateToken', '')
        //跳转到登录页面
        this.$router.push('/login')
      }).catch(() => {
        this.$message.info('已取消')
      })


        ;
    },
    // 获取菜单数据
    async initMenu () {
      const ret = await this.$http.get('/my/menus', {
        headers: {
          Authorization: this.token
        }
      })
      if (ret.data.code === 0) {
        this.menus = ret.data.data
        //console.log(this.menus);
      }
    },
  },
  created () {

    if (this.token) {
      this.$store.dispatch('getUserInfo')
      this.initMenu()
    } else {
      this.$router.push('/')
    }

  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    .el-submenu,
    .el-menu-item {
      width: 200px;
      user-select: none;
    }
    background-color: #23262e;
    .user-box {
      height: 70px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-top: 1px solid #000;
      border-bottom: 1px solid #000;
      user-select: none;
      img {
        width: 35px;
        height: 35px;
        border-radius: 50%;
        background-color: #fff;
        margin-right: 15px;
        object-fit: cover;
      }
      span {
        color: white;
        font-size: 12px;
      }
    }
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
